﻿using System;
using System.Data;
using System.Runtime.InteropServices;
using System.Web;
using System.Web.SessionState;
using System.Collections;
using System.Web.UI.WebControls;
using DreamBuilder.DictionaryAccess;

namespace DreamBuilder.UI.UserControls
{
    public class ListHyperLinkField : HyperLinkField
    {
        public ListHyperLinkField() { }
        public ListHyperLinkField(CVoucherListItemInfo listItemInfo, SortData sortData)
        {
            FieldDicInfo fieldDicInfo = DictionaryAccessFacade.GetFieldDicInfo(listItemInfo.TableCode, listItemInfo.FieldCode);
            string sFormatString = ControlCommon.GetDataFormatType(fieldDicInfo);
            if (sFormatString.Trim().Length > 0)
            {
                this.DataTextFormatString = sFormatString;
            }
            ListHelper.SetGridViewColumnHeaderText(listItemInfo, this, sortData);
            ListHelper.SetGridViewColumnStyle(listItemInfo, this);
            this.DataTextField = listItemInfo.TableCode + "__" + listItemInfo.FieldCode;

            if (listItemInfo.NavigateURL.Length == 0)
            {
                if (fieldDicInfo.FieldType == FIELDTYPE.TYPE_Reference || fieldDicInfo.FieldType == FIELDTYPE.TYPE_Bool)
                {
                    this.DataNavigateUrlFields = new string[] { listItemInfo.TableCode + "." + listItemInfo.FieldCode };
                }
                else
                {
                    this.DataNavigateUrlFields = new string[] { listItemInfo.TableCode + "__" + listItemInfo.FieldCode };
                }
                string currentUrl = System.Web.HttpContext.Current.Request.Url.ToString();
                int m = currentUrl.IndexOf("DoSearchSql");
                if (m > 0)
                {
                    currentUrl = currentUrl.Remove(m - 1);
                }
                int n = currentUrl.IndexOf("?");
                if (n > 0)
                {
                    currentUrl = currentUrl + "&";
                }
                else
                {
                    currentUrl = currentUrl + "?";
                }
                if (fieldDicInfo.FieldType == FIELDTYPE.TYPE_Bool
                    || fieldDicInfo.FieldType == FIELDTYPE.TYPE_Decimal
                    || fieldDicInfo.FieldType == FIELDTYPE.TYPE_Integer
                     || fieldDicInfo.FieldType == FIELDTYPE.TYPE_Float
                    )
                {
                    this.DataNavigateUrlFormatString = currentUrl + "DoSearchSql=" + listItemInfo.TableCode + "." + listItemInfo.FieldCode + "={0}";
                }
                else
                {
                    this.DataNavigateUrlFormatString = currentUrl + "DoSearchSql=" + listItemInfo.TableCode + "." + listItemInfo.FieldCode + "='{0}'";
                }
            }
            else
            {
                if (fieldDicInfo.FieldType == FIELDTYPE.TYPE_Reference)
                {
                    this.DataNavigateUrlFields = new string[] { listItemInfo.TableCode + "." + listItemInfo.FieldCode };
                }
                else
                {
                    this.DataNavigateUrlFields = new string[] { listItemInfo.TableCode + "__" + listItemInfo.FieldCode };
                }
                this.DataNavigateUrlFormatString = listItemInfo.NavigateURL + "?" + listItemInfo.NavigateFields + "={0}";
                this.Target = "_blank ";
            }
        }
    }
}